﻿app.controller('WarningIconController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.AddIcon = function (model) {
        if (model.TypeName == null || model.TypeName == '') {
            alertify.alert("请输入类型名称");
            return false;
        }
        var files = document.querySelector('input[type=file]').files;
        if (files.length == 0) {
            alertify.alert("请选择图片文件");
            return false;
        }
        $scope.DataLoading = true;
        Upload.upload({
            method: 'POST',
            url: '../WarningIcon/AddIcon',
            data: { 'model': model },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                alertify.alert("上传成功！");
                setTimeout(function () {
                    location.href = "WarningIconPageList?page=1";
                }, 2000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.DeleteIcon = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../WarningIcon/DeleteIcon',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox(ret.Msg);
                            setTimeout(function () {
                                $scope.GetIconPageList(model, page);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.EditIcon = function (model, page) {
        if (model.TypeName == null || model.TypeName == '') {
            alertify.alert("请输入类型名称");
            return false;
        }
        var files = document.querySelector('input[type=file]').files;
        $scope.DataLoading = true;
        Upload.upload({
            method: 'POST',
            url: '../WarningIcon/EditIcon',
            data: { 'model': model },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                alertify.alert("上传成功！");
                setTimeout(function () {
                    location.href = "WarningIconPageList?page="+page;
                }, 2000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.GetIconPageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.TypeList = [];
        $http({
            method: 'POST',
            url: '../WarningIcon/GetIconPageList',
            data: { 'Name': model.Name, 'page': page }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.TypeList = ret.Data.Data;
                $scope.TotalCount = ret.Data.TotalItemCount;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.GetIconDetail = function (id) {
        $scope.EquTypeDetail = {};
        $http({
            method: 'POST',
            url: '../WarningIcon/GetIconDetail',
            data: { 'Id': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EquTypeDetail = ret.Data;
                    $scope.EquTypeDetail.CreateTime = $filter('JSONDate')($scope.EquTypeDetail.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

});